package com.maserb.monitor.service.schoolNotice.impl;

import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolNotice.SchoolNoticeEntity;
import com.maserb.monitor.entity.schoolNotice.SchoolNoticeHistoryEntity;
import com.maserb.monitor.repository.schoolNotice.SchoolNoticeHistoryRepository;
import com.maserb.monitor.repository.schoolNotice.SchoolNoticeRepository;
import com.maserb.monitor.repository.schoolOrg.SchoolUserOrgRepository;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolNotice.SchoolNoticeHistoryService;
import com.maserb.monitor.service.schoolNotice.SchoolNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SchoolNoticeHistoryServiceImpl extends JpaSpecificationServiceImpl<SchoolNoticeHistoryRepository,
        SchoolNoticeHistoryRepository, SchoolNoticeHistoryEntity, String>
        implements SchoolNoticeHistoryService {

    @Autowired
    UserService userService;

    @Autowired
    JwOrgService orgService;

    @Autowired
    SchoolUserOrgRepository userOrgRepository;

    @Autowired
    IToolService toolService;

    private Specification<SchoolNoticeEntity> getSpecification(JwOrgEntity orgEntity,String type, String keyword) {
        return new Specification<SchoolNoticeEntity>()  {
            @Override
            public Predicate toPredicate(Root<SchoolNoticeEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathTitle = root.get("title");
                Path<String> pathType = root.get("type");
                Path<JwOrgEntity> pathOrg = root.get("org");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(keyword)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathTitle, "%" + keyword + "%")));
                }

                if(!StringUtils.isEmptyOrWhitespace(type)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathType, type)));
                }
                if(null!=orgEntity && !StringUtils.isEmptyOrWhitespace(orgEntity.getId())){
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathOrg,orgEntity)));
                }

                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }

   /* @Override
    public Page<SchoolNoticeEntity> findAll(Pageable pageable,String orgId, String type, String keyword) {
        JwOrgEntity orgEntity = new JwOrgEntity();
        if(!StringUtils.isEmptyOrWhitespace(orgId)){
            orgEntity = orgService.findOne(orgId);
        }

        return this.findAll(getSpecification(orgEntity,type, keyword), pageable);
    }

    @Override
    public Page<SchoolNoticeEntity> findAll(Pageable pageable, String userId, String clazzId, String type, String keyword,String startTime, String endTime) {
        UserEntity user = new UserEntity();
        List<JwOrgEntity> orgList = new ArrayList<JwOrgEntity>();
        List<String>orgIdList = new ArrayList<String>();
        if(!StringUtils.isEmptyOrWhitespace(userId)){
            user = userService.findOne(userId);
            if(null!=user &&!"".equals( user.getId())){
                orgList = orgService.findByUser(user);
                if(orgList.size()>0){
                    for(JwOrgEntity org:orgList){
                        orgIdList.add(org.getId());
                    }
                }
                orgIdList.add(user.getOrgId());
            }
        }
        return this.findAll(getSpecificationByUserId(orgIdList,clazzId, type, keyword,startTime,endTime),pageable);
    }*/

    private Specification<SchoolNoticeEntity> getSpecificationByUserId(List<String> orgIdList, String clazzId, String type, String keyword,String startTime, String endTime){
        return new Specification<SchoolNoticeEntity>() {
            @Override
            public Predicate toPredicate(Root<SchoolNoticeEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                Join<SchoolNoticeEntity,JwOrgEntity>joinOrg = root.join("org",JoinType.LEFT);
                Path<String> pathJoinOrg = joinOrg.get("id");
                list.add(criteriaBuilder.and(criteriaBuilder.equal(pathJoinOrg, root.get("org"))));
                Path<Date> pathCreateTime = root.get("createTime");
                Path<String>pathType = root.get("type");
                Path<String> pathTitle = root.get("title");
                if (null!=orgIdList && orgIdList.size()>0) {
                    Expression<String> exp = pathJoinOrg;
                    list.add(exp.in(orgIdList));
                }
                if (!StringUtils.isEmptyOrWhitespace(clazzId)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathJoinOrg, clazzId)));
                }
                if (!StringUtils.isEmptyOrWhitespace(type)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathType, type)));
                }
                if (!StringUtils.isEmptyOrWhitespace(keyword)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathTitle, "%" + keyword + "%")));
                }
                if(!StringUtils.isEmptyOrWhitespace(startTime)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.greaterThanOrEqualTo(pathCreateTime, toolService.parseDateFromNormalDateFormat(startTime))));
                }
                if(!StringUtils.isEmptyOrWhitespace(endTime)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.lessThanOrEqualTo(pathCreateTime, toolService.parseDateFromNormalDateFormat(endTime))));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }
}